package com.frogs.ucenter.controller;

import com.frogs.framework.base.web.BaseController3;
import com.frogs.framework.util.DWZResponse;
import com.frogs.framework.util.PageList;
import com.frogs.framework.util.RenderUtil;
import com.frogs.framework.web.HttpRequestInfo;
import com.frogs.ucenter.entity.PharmacistApply;
import com.frogs.ucenter.service.PharmacistApplyService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.PropertyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/admin/pharmacistApply")
public class PharmacistApplyController extends BaseController3 {
    private static final String ROOT_VIEW = "admin/pharmacistApply";

    private static final String HOME_VIEW = "admin/pharmacistApply/home";

    private static final String EDIT_VIEW = "admin/pharmacistApply/edit";

    @Autowired
    private PharmacistApplyService pharmacistApplyService;

    @RequestMapping()
    public String listPharmacistApply(HttpServletRequest request, ModelMap model) {
        HttpRequestInfo reqInfo = new HttpRequestInfo(request);
        setRequestModelMap( request, model, true );
        getPageList( model );
        return HOME_VIEW;
    }

    @RequestMapping(EDIT)
    public String createPharmacistApply(HttpServletRequest request, ModelMap model) {
        HttpRequestInfo reqInfo = new HttpRequestInfo(request);
        setRequestModelMap( request, model, false );
        Integer id = reqInfo.getIntParameter( "id", -1 );
        if( id > 0 ){
            model.put( "model",pharmacistApplyService.getById( id ) ); 
        }
        return EDIT_VIEW;
    }

    @RequestMapping(ADD)
    public String savePharmacistApply(HttpServletRequest request, HttpServletResponse response, ModelMap model, PharmacistApply pharmacistApply) {
        setRequestModelMap(request, model, false);
        DWZResponse.Builder builder;
        try {
            pharmacistApplyService.insert( pharmacistApply ); 
            builder = DWZResponse.getSucessBuilder("success");
        } catch (Exception e) {
            builder = DWZResponse.getFailBuilder("fail" + e.getMessage());
        }
        RenderUtil.renderHtml(builder.build().toString(), response);
        return null;
    }

    @RequestMapping(SHOW)
    public String showPharmacistApply(HttpServletRequest request, ModelMap model) {
        HttpRequestInfo reqInfo = new HttpRequestInfo(request);
        Integer id = reqInfo.getIntParameter( "id", -1 );
        if( id > 0 ){
            model.addAttribute( pharmacistApplyService.getById( id ) ); 
        }
        setRequestModelMap(request, model);
        return null;
    }

    @RequestMapping(UPDATE)
    public String updatePharmacistApply(HttpServletRequest request, HttpServletResponse response, ModelMap model, PharmacistApply pharmacistApplyNew) throws Exception {
        HttpRequestInfo reqInfo = new HttpRequestInfo(request);
        Integer id = reqInfo.getIntParameter( "id", -1 );
        if( id > 0 ){
            DWZResponse.Builder builder;
            try {
                PharmacistApply pharmacistApply = pharmacistApplyService.getById( id ); 
                PropertyUtils.copyProperties(pharmacistApply, pharmacistApplyNew);
                pharmacistApplyService.update( pharmacistApply ); 
                model.clear();
                builder = DWZResponse.getSucessBuilder("success");
            } catch (Exception e) {
                builder = DWZResponse.getFailBuilder("fail" + e.getMessage());
            }
            RenderUtil.renderHtml(builder.build().toString(), response);
        }
        return null;
    }

    @RequestMapping(AUDIT)
    public String auditPharmacistApply(HttpServletRequest request, HttpServletResponse response, ModelMap model, String inIdList) {
        HttpRequestInfo reqInfo = new HttpRequestInfo(request);
        Integer status = reqInfo.getIntParameter( "status", -1 );
        
        setRequestModelMap(request, model);
        DWZResponse.Builder builder;
        try {
            String[] idArray = inIdList.split(",");
            model.put("inIdList", idArray);
            pharmacistApplyService.update( model ); 
            builder = DWZResponse.getSucessBuilder("ɾ����Ϣ�ɹ�");
        } catch (Exception e) {
            builder = DWZResponse.getFailBuilder("ɾ����Ϣʧ�ܣ�" + e.getMessage());
        }
        RenderUtil.renderHtml(builder.build().toString(), response);
        model.clear();
        return null;
    }

    @RequestMapping(DELETE)
    public String deletePharmacistApply(HttpServletRequest request, HttpServletResponse response, ModelMap model, String inIdList) {
        HttpRequestInfo reqInfo = new HttpRequestInfo(request);
        setRequestModelMap(request, model);
        DWZResponse.Builder builder;
        try {
            String[] idArray = inIdList.split(",");
            model.put("inIdList", idArray);
            pharmacistApplyService.delete( model ); 
            builder = DWZResponse.getSucessBuilder("ɾ����Ϣ�ɹ�");
        } catch (Exception e) {
            builder = DWZResponse.getFailBuilder("ɾ����Ϣʧ�ܣ�" + e.getMessage());
        }
        RenderUtil.renderHtml(builder.build().toString(), response);
        model.clear();
        return null;
    }

    private PageList<PharmacistApply> getPageList(ModelMap model) {
        PageList<PharmacistApply> pharmacistApplyList = pharmacistApplyService.getList( model, Integer.parseInt(model.get("pageNum").toString()), Integer.parseInt(model.get("numPerPage").toString()) ); 
        model.put("pharmacistApplyList",pharmacistApplyList);
        model.put("pageTurn",pharmacistApplyList.getPageTurn());
        return pharmacistApplyList;
    }
}